﻿@{
    var Productlist = new ProductBLL().GetForSelect(new SelectListItem() { Text = "--全部产品--", Value = "-1" });
    if (Request.Form["ProductID"] != null)
    {
        Productlist.First(i => i.Value == Request.Form["ProductID"]).Selected = true;
    }
    var Statelist = typeof(SaleOrderState).EnumToSelectItems(new SelectListItem() { Text = "全部状态", Value = "-1" });
    if (Request.Form["State"] != null)
    {
        Statelist.First(i => i.Value == Request.Form["State"]).Selected = true;
    }
    var Customerlist = new CustomerBLL().GetForSelect(new List<CustomerType>() { CustomerType.代理商, CustomerType.零售商 }, new SelectListItem() { Text = "--全部顾客--", Value = "-1" });
    if (Request.Form["Customer"] != null)
    {
        Customerlist.First(i => i.Value == Request.Form["Customer"]).Selected = true;
    }
}
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <div class="widget-box">
                <form action="@Url.Action("List", "SaleOrder")" method="post">
                <div class="widget-title">
                    <span class="icon"><i class="icon-th"></i></span>
                    <h5>
                        出货单列表</h5>
                </div>
                <div class="widget-content" style="height: 25px;">
                    <a href="@Url.Action("Info", "SaleOrder")" class="btn btn-success fl">添 加</a>
                    <input type="button" value="批量删除" class="btn btn-danger fl" onclick="Deletes()" />
                    <div class="span3">
                        @Html.DropDownList("ProductID", Productlist)
                    </div>
                    <div class="span2">
                        @Html.DropDownList("Customer", Customerlist)
                    </div>
                    <div class="span2">
                        @Html.DropDownList("State", Statelist)
                    </div>
                    <div class="span2">
                        @Html.TextBox("OrderDate", Request.Form["OrderDate"], new Dictionary<string, object>() { { "class", "datepicker span2" }, { "data-date-format", "yyyy-mm-dd" }, { "style", "width:95px" }, { "placeholder", "下单日期" } })
                    </div>
                    <button type="submit" class="btn btn-info fl" onclick="return Search();">
                        搜 索</button>
                </div>
                <div class="widget-content">
                    <table class="table table-bordered table-striped with-check">
                        <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" id="title-table-checkbox" name="title-table-checkbox" />
                                </th>
                                <th style="width: 150px;">
                                    产品名称
                                </th>
                                <th style="width: 100px;">
                                    顾客
                                </th>
                                <th>
                                    数量
                                </th>
                                <th>
                                    单价
                                </th>
                                <th>
                                    快递费
                                </th>
                                <th>
                                    其他费用
                                </th>
                                <th>
                                    总价
                                </th>
                                <th>
                                    下单时间
                                </th>
                                <th style="width: 85px;">
                                    支付状态
                                </th>
                                <th style="width: 85px;">
                                    订单状态
                                </th>
                                <th style="width: 70px;">
                                    操作
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (SaleOrderList item in ViewBag.PagList.DataList)
                            {
                                <tr>
                                    <td>
                                        <input type="checkbox" name="ID" value="@item.ID" />
                                    </td>
                                    <td>
                                        <a target="_blank" title="@item.CreateTime" href="@Url.Action("Info", "Product", new { id = @item.ProductID })">@item.ProductName</a>
                                    </td>
                                    <td>
                                        @item.CustomerName
                                    </td>
                                    <td>
                                        @item.Num
                                    </td>
                                    <td>
                                        <font color="red">@((item.Price ?? 0).ToString("0.00"))
                                            元</font>
                                    </td>
                                    <td>
                                        <font color="red">@((item.ExpressPrice ?? 0).ToString("0.00"))
                                            元</font>
                                    </td>
                                    <td>
                                        <font color="red">@((item.OtherPrice ?? 0).ToString("0.00"))
                                            元</font>
                                    </td>
                                    <td>
                                        <font color="red">@((item.AllPrice ?? 0).ToString("0.00"))
                                            元</font>
                                    </td>
                                    <td>
                                        @item.OrderTime.ToShortStr()
                                    </td>
                                    <td>
                                        <input data-content="付款时间：@item.ReceivedMoneyTime.ToShortStr()" data-placement="top" data-toggle="popover" data-original-title="付款具体时间" class="btn btn-mini popoverbtn @(@item.PayState == 1 ? "btn-danger" : "btn-success")" onclick="SetPayState(@item.ID,@item.PayState)"  type="button" value="@item.PayStateName"  />
                                    </td>
                                    <td>
                                        <input data-content="发货时间：@item.SendTime.ToShortStr()" data-placement="top" data-toggle="popover" data-original-title="货运具体时间" class="btn btn-mini popoverbtn @(item.State == 1 ? "btn-inverse" : "")" onclick="SetState(@item.ID,@item.State)"  type="button" value="@item.StateName"  />
                                        @if ((SaleOrderState)item.State == SaleOrderState.已发货)
                                        {   
                                            <i class="icon-legal" title="点击退货" style="cursor: pointer" onclick="Back(@item.ID)">
                                            </i>
                                        }
                                    </td>
                                    <td style="text-align: center; line-height: 25px;">
                                        <a class="btn btn-primary btn-mini" href="@Url.Action("Info", "SaleOrder", new { id = item.ID })">
                                            编 辑&nbsp;</a>
                                        <input  type="button" value=" 删 除 " class="btn btn-danger btn-mini" onclick="Delete(@item.ID)" />
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                    @Html.Partial("_Paging")
                </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    //删除单个
    function Delete(id) {
        if (confirm("确认删除？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("Delete", "SaleOrder")', "id=" + id, function (d) {
                $("#LoadingBox").hide();
                if (d == "False") {
                    alert("订单状态为待发货才能删除！");
                }
                else {                    
                    $("form").submit();
                }               
            })
        }
    }
    //批量删除
    function Deletes() {
        var obj = $("input:checkbox[name='ID']:checked");
        if (obj.length == 0) { alert("请至少选择一条数据！"); return; }
        var ids = ""; obj.each(function () { ids += $(this).val() + ","; });
        if (confirm("确认删除" + obj.length + "条数据？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("Delete", "SaleOrder")', "id=" + ids, function (d) {
                $("#LoadingBox").hide();
                //刷新页面，页码变为第一页
                $('#PageIndex').val(1);
                $("form").submit();
            })
        }
    }
    //设置付款状态
    function SetPayState(id, state) {
        if (state == 1 && confirm("确定修改状态为已付款？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("SetPayState", "SaleOrder")', "id=" + id, function (d) {
                $("#LoadingBox").hide();
                $("form").submit();
            })
        }
    }
    //设置出货状态
    function SetState(id, state) {
        if (state == 1 && confirm("确定修改状态为已发货？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("SetState", "SaleOrder")', "id=" + id, function (d) {
                $("#LoadingBox").hide();
                $("form").submit();
            })
        }
    }
    //设置退货状态
    function Back(id) {
        if (confirm("确定将此单退货？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("Back", "SaleOrder")', "id=" + id, function (d) {
                $("#LoadingBox").hide();
                $("form").submit();
            })
        }
    }
</script>
